package com.bidevalution.business.function;

import java.util.HashMap;
import java.util.Map;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;
/**
 * 执行宏定义函数
 * @author daizhifeng
 * @date 2019-08-06
 */
public class MacroExecutorFunction extends AbstractFunction{
	private FunctionInitializer functionInitializer;
	public MacroExecutorFunction(FunctionInitializer functionInitializer) {
		super();
		this.functionInitializer = functionInitializer;
	}
	@Override
	public AviatorObject call(Map<String, Object> env, AviatorObject arg1){
		Integer macroCode = Integer.valueOf(arg1.getValue(env).toString());
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("code", macroCode);
		String config = functionInitializer.getJdbcTemplateService().selectForObject("select config from be_function_macro where code=:code", paramMap,String.class);
		return new AviatorString(FunctionCalculator.computeAviator(config, env).toString());
	}
	@Override
	public String getName() {
		return "macroExecutor";
	}

}
